<div class="content">

    <div class="list_button">
        <table cellpadding="0" cellspacing="0" border="0" width="100%" height="100%">
        <tr>
            <td>
                <?php
					echo CHtml::link(
						"<div class='button'>" .
							"<div style='float:left; width:20px;'>" .
								CHtml::image(Yii::app()->baseUrl."/assets/icon/icon-16-plus.png") .
							"</div>" .
							"<div style='float:left; width:80px;'>".
								"Thêm mới" .
							"</div>" .
						"</div>"
						,
                       array($this->controller_id."/add", "page"=>$page)
                    );
					
					echo "<div class='button' id='delete_multi'>" .
							"<div style='float:left; width:20px;'>" .
								CHtml::image(Yii::app()->baseUrl."/assets/icon/icon-16-remove.png") .
							"</div>" .
							"<div style='float:left; width:40px;'>".
								"Xóa" .
							"</div>" .
						"</div>";
					
                    echo "<div style='color:#f00; font-size:12px; padding-top:7px;'>";
						echo "<strong>* Chú ý:</strong> Chỉ xóa các nhóm rỗng.";
					echo "</div>";
                ?>
                <script language="javascript">
                $(document).ready(function(){
                    $('#delete_multi').click(function(){
                        var form = document.getElementById("form_table_all");
                        form.submit();
                    });	
                })
              </script>
            </td>
            
            <td align="right">
                <?php
					$list_group = array();
                    $list_group[0] = "-- Nhóm --";
                    $groups = group::model()->findAll(array("order"=>"name ASC"));
                    if (!is_null($groups))
                    {
                        foreach($groups as $group)
                        {
                            $list_group[$group["id"]] = $group["name"];
                        }
                    }
                    
				?>
             </td>
          </tr>
        </table>
    </div>



<table class="view">
    <tr height='25'>
        <th width="50px"> # </th>
        <th width="30">
            <?php 
                echo CHtml::checkBox("checkbox_all", false, array("id"=>"checkbox_all"));
            ?>
        </th>
        <th colspan="2">Action</th>
        <th>Nhóm</th>
        <th width="80">Bật/Tắt</th>
        <th width="80"># User</th>
        <th width="100">
        	<?php 
				echo CHtml::link(
					"Thứ tự",
					array($this->controller_id."/set_order"),
					array("title"=>"Tạo thứ tự mặc định")
				);
				echo " ";
				echo CHtml::image(Yii::app()->baseUrl."/assets/icon/icon-16-saveorder.png", "", 
					array("style"=>"cursor:pointer", "id"=>"id_save_order"));

			?>
        </th>
        <script language="javascript">
			$(document).ready(function(){
				$('#id_save_order').click(function() {
					var form = document.getElementById("form_table_all");
					form.action = '<?php
						$url = Yii::app()->createUrl(
							$this->route."/save_order", 
							array("page" => $page)
						);
						echo $url;
					?>';
					form.submit();
					//alert(form.action);
				});
			});
		</script>
        <th width="120">Alias</th>
        <th width="100">Loại</th>
        <th width="120">Nhóm cha</th>
        <th width="50">ID</th>
    </tr>
    <?php
        if (! is_null($rows))
        {
            echo CHtml::beginForm(
				array($this->controller_id."/delete_multi", "page"=>$page),
				"post",
                array("id"=>"form_table_all")
			);
            
            $i = 1;
            foreach($rows as $row)
            {
                echo "<tr>";
                    echo "<td align='center'>";
                        echo $i++;
                    echo "</td>";
                    
                    echo "<td align='center'>";
                    	echo CHtml::checkBox("cib[".$row["id"]."]", false, array("id"=>"cb".$i));
                    echo "</td>";
					
					echo "<td width='26px' align='center'>";
						$count_user = user::model()->count(array("condition" => "group_id = " . $row->id));
						
						if ($count_user == 0)
						{
							$url = Yii::app()->createUrl(
								$this->route."/delete",
								array(
									"group_id" => $row->id,
									"page" => $page
								)
							);
							
							echo CHtml::image(
								Yii::app()->baseUrl.'/assets/icon/icon-16-trash.png',
								"",
								array(
									"style"=>"cursor:pointer",
									"title"=>"Delete",  
									"onClick"=>"javascript:actionDelete('".$url."');"
								)
							);
						}
						else
						{
							$message = "Bạn không thể xóa nhóm này vì nhóm không rỗng.";
							echo CHtml::image(
								Yii::app()->baseUrl.'/assets/icon/icon-16-trash.png',
								"",
								array(
									"style"=>"cursor:pointer",
									"title"=>"Delete",  
									"onClick"=>"javascript:alertMessage('".$message."');"
								)
							);
						}
							
					echo "</td>";
					
					echo "<td width='26px' align='center'>";
						echo CHtml::link(
							CHtml::image(Yii::app()->baseUrl.'/assets/icon/icon-16-edit.png'),
							array(
								$this->controller_id."/edit",
								 'group_id' => $row->id,
								 "page" => $page
							),
							array('title'=>'edit')
						);
					echo "</td>";
						
                    echo "<td>";
                        echo CHtml::link(	
                            $row["name"],
                            array(
                                $this->controller_id."/edit",
                                'group_id' => $row->id,
								"page" => $page
                            ),
                            array('title'=>'Edit')
                        );
                    echo "</td>";
                    
                    echo "<td align='center'>";
                        $icons =  array(
                            0 => "icon-16-remove.png",
                            1 => "icon-16-tick.png",
                        );
                        echo CHtml::link(
                            CHtml::image(Yii::app()->baseUrl.'/assets/icon/'.$icons[$row["enable"]]),
                            array($this->controller_id."/enable", 
                               'group_id' => $row->id,
								"page" => $page
                            )
                        );
                    echo "</td>";
                    
                    echo "<td align='center'>";
                       //$count_user = user::model()->count(array("condition" => "group_id = " . $row->id));
					   echo "<span style='color:red;'>".$count_user ."</span>";
                    echo "</td>";
                    
                    echo "<td align='right'>";
                        echo CHtml::textField(
							"order[".$row->id."]",
							$row->order,
							array("style"=>"width:40px; text-align:right; 
								padding:3px; color:#f00; font-size:11px;")
						);
						echo " ";
						
						
						echo CHtml::link(
							CHtml::image(Yii::app()->baseUrl."/assets/icon/icon-16-up.png"),
							array(
								$this->controller_id."/up",
								'group_id' => $row->id,
								"page" => $page
							)
						);
						
						echo CHtml::link(
							CHtml::image(Yii::app()->baseUrl."/assets/icon/icon-16-down.png"),
							array(
								$this->controller_id."/down",
								'group_id' => $row->id,
								"page" => $page
							)
						);
                    echo "</td>";
                    
					echo "<td>";
						 echo "<span style='color:#066195;'>";
						 	echo $row->alias;
						 echo "</span>";
					echo "</td>";
					
                    echo "<td align='center'>";
                    	if ($row->backend == 1)
							echo "<span style='color:red;'>"."Back-end" ."</span>";
						else
							echo "<span style='color:#217f00;'>"."Front-end"."</span>";
						
                    echo "</td>";
              
					echo "<td>";
						$parent = group::model()->findByPk($row->parent_id);
						if (is_null($parent))
							echo "<span style='color:red'>NULL</span>";
						else
							echo $parent->name;
					echo "</td>";
					
                    echo "<td align='center'>";
                        echo $row["id"];
                    echo "</td>";
                echo "</tr>";        
            }
            
            echo CHtml::endForm(); // end form
        }
    ?>
</table>
</div><!-- end div content -->
<?php 
	// display pagination
	if ($pages->pageCount > 1)
	{
		echo "<div class='pagination'>";
			$this->widget('CLinkPager', array(
				'header' => "",
				'cssFile' => Yii::app()->baseUrl."/assets/css/pager.css",
				"prevPageLabel" => "«",
				"nextPageLabel" => "»",
				'pages' => $pages,
			));
			
			$this->widget('CListPager', array(
				//"promptText" => "Chuyển nhanh",
				"header" => "Trang ",
				'pages'=>$pages
			));
		echo "</div>";
	}
?>

<script language="javascript">
function actionDelete(url)
{	
	if (confirm("Bạn muốn xóa mẫu tin này ?") == true)
		window.location = url;
}

function alertMessage(message)
{
	alert(message);
}
</script>
